package com.it.test;


import com.it.config.controller.spring_02_ioc_bean;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class spring_02_text {
    /*
     * 【测试】：组件（Bean）的声明及配置
     * */

    // 测试：依赖注入：构造器注入
    @Test
    public void test01() {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring_ioc_02.xml");
        spring_02_ioc_bean cpxBean = cpx.getBean(spring_02_ioc_bean.class);
        System.out.println(cpxBean.toString());
        cpxBean.getSpring01IocXml().method();
        cpx.close();
    }

    // 测试：依赖注入：构造器注入
    @Test
    public void test02() {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring_ioc_02.xml");
        // 当ioc容器中出现多个同一类型Bean，建议使用 id + class 的获取方式
        spring_02_ioc_bean iocBean2 = cpx.getBean("spring02IocBean2", spring_02_ioc_bean.class);
        System.out.println(iocBean2.toString());
        iocBean2.getSpring01IocXml().method();
        cpx.close();
    }

    // 测试：作用域概念
    @Test
    public void test03() {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring_ioc_02.xml");
        spring_02_ioc_bean iocBeanScope = cpx.getBean("spring02IocBeanScope", spring_02_ioc_bean.class);
        spring_02_ioc_bean iocBeanScope1 = cpx.getBean("spring02IocBeanScope", spring_02_ioc_bean.class);

        System.out.println(iocBeanScope.getClass());
        System.out.println(iocBeanScope1.getClass());
        // 当设置
        System.out.println(iocBeanScope == iocBeanScope1);
    }


    // 测试：周期方法
    @Test
    public void test04() {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring_ioc_02.xml");
        spring_02_ioc_bean beanMethod = cpx.getBean("spring02IocBeanMethod", spring_02_ioc_bean.class);

        System.out.println(beanMethod.toString());

        cpx.close();
        /*
        * 执行过程
        *   spring_02_ioc_bean：初始化方法
        *   spring_02_ioc_bean(name=null, age=0, spring01IocXml=null)
        *   spring_02_ioc_bean：销毁方法
         * */

    }

}
